/*
 *  SRVector.cpp
 *  softwarerenderer
 *
 *  Created by Johannes Loepelmann on 22.02.09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */
#include <math.h>
#include "SRVector.h"

SRVector4::SRVector4()
	{
		x=y=z=w=0.0f;
	}

SRVector4::SRVector4(float xx, float yy, float zz, float ww)
	{
		set(xx,yy,zz,ww);
	}

void SRVector4::set(float xx, float yy, float zz, float ww)
{
	x = xx;
	y = yy;
	z = zz;
	w = ww;
}

float SRVector4::lengthSquared()
	{
		return x*x+y*y+z*z+w*w;
	}

float SRVector4::length()
{
	return sqrt(lengthSquared());
}

void SRVector4::normalize()
{
	float len = length();
	
	if(len>0){
		x /= len;
		y /= len;
		z /= len;
		w /= len;
		
	}
}

float SRVector4::dot(SRVector4* vec)
{
	return x*vec->x+y*vec->y+z*vec->z+w*vec->w;
}

SRVector4* SRVector4::cross(SRVector4* vec)
{
	SRVector4* ret = 	new SRVector4(y * vec->z - z * vec->y, z * vec->x - x * vec->z, x * vec->y - y * vec->x, 1.0f);
	return ret;
}

SRVector4 & SRVector4::operator +=(const SRVector4 &rhs)
{
	x += rhs.x;
	y += rhs.y;
	z += rhs.z;
	w += rhs.w;
	
	return *this;
}

SRVector4 & SRVector4::operator -=(const SRVector4 &rhs)
{
	x -= rhs.x;
	y -= rhs.y;
	z -= rhs.z;
	w -= rhs.w;
	
	return *this;
}

SRVector4 & SRVector4::operator *=(const float s)
{
	x *= s;
	y *= s;
	z *= s;
	w *= s;
	
	return *this;
}

SRVector4 & SRVector4::operator /=(const float s)
{
	x /= s;
	y /= s;
	z /= s;
	w /= s;
	
	return *this;
}
